package com.baymax.test.scene.demo

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.Message
import android.os.Messenger
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.internal.BackGround
import com.baymax.test.scene.newSceneFlexContainer
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.rx2.asCoroutineDispatcher

class KillSecondScene : BaseScene() {
    override fun onCreateView(
        p0: LayoutInflater,
        p1: ViewGroup,
        p2: Bundle?
    ) = p1.newSceneFlexContainer {
        val sercon = object : ServiceConnection {
            override fun onServiceConnected(
                name: ComponentName?,
                service: IBinder?
            ) {
                println("serConn $name ,$service")
                Messenger(service).send(
                    Message.obtain(
                        null,
                        1,
                        "hello"
                    )
                )

            }

            override fun onServiceDisconnected(name: ComponentName?) {

            }
        }
        addButton("start service") {
            val intent = Intent(requireSceneContext(), BackGround::class.java)
            ContextCompat.startForegroundService(requireSceneContext(), intent)
            requireSceneContext().bindService(intent, sercon, Context.BIND_AUTO_CREATE)
        }
    }

}